// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: jhkim

#include "coin2/base/log.h"
#include "spdlog/cfg/env.h"

namespace coin2::base {
// export SPDLOG_LEVEL="off"
// export SPDLOG_LEVEL="on"

void InitLogging(const char* app_name, const ::coin2::app::LogConfig& config) {
  google::InitGoogleLogging(app_name);
  LOG(INFO) << "[InitLogging] app_name=" << app_name;

  if (config.has_log_file()) {
    std::experimental::filesystem::path path = config.log_file();
    LOG(INFO) << "[InitLogging] log_file=" << path;
    if (config.log_file() != "") {
      if (std::experimental::filesystem::create_directories(path.parent_path())) {
        LOG(INFO) << "[InitLogging] created dir: " << path.parent_path();
      }
    }
    google::SetLogDestination(google::INFO, path.c_str());
    google::SetLogDestination(google::WARNING, "");  // GLOG does not write if empty string
    google::SetLogDestination(google::ERROR, "");    // GLOG does not write if empty string
    google::SetLogDestination(google::FATAL, "");    // GLOG does not write if empty string
  }
  spdlog::cfg::load_env_levels();
}
}  // namespace coin2::base
